package dp;

/**
 * https://leetcode.cn/problems/perfect-squares/?envType=study-plan-v2&envId=top-100-liked
 */
public class _279_完全平方数 {

    /**
     * 题解：https://leetcode.cn/problems/perfect-squares/solutions/822940/wan-quan-ping-fang-shu-by-leetcode-solut-t99c/
     *
     * @param n
     * @return
     */
    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            Integer min = Integer.MAX_VALUE;
            for (int j = 1; j * j < i; j++) {
                min = Math.min(min, dp[i - j * j]);
            }
            dp[i] = min + 1;
        }

        return dp[n];
    }
}
